package com.xbai.intervention.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xbai.intervention.client.UserMapper;
import com.xbai.intervention.domain.Intervention;
import com.xbai.intervention.service.InterventionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author jxbai
 * @desc
 * @date 2021/1/15 0015
 */
@RestController
@RequestMapping("/intervention")
@Slf4j
public class InterventionController {

    @Autowired
    private InterventionService interventionService;

    @GetMapping("/{userName}/{deviceCode}")
//    @HystrixCommand(fallbackMethod = "generateInterventionFallback")
    public Intervention generateIntervention(@PathVariable("userName") String userName,
                                             @PathVariable("deviceCode") String deviceCode) {
        log.info("Generate intervention for userName: {} and deviceCode: {}.", userName, deviceCode);
        return interventionService.generateIntervention(userName, deviceCode);
    }

    private Intervention generateInterventionFallback(String userName, String deviceCode) {
        Intervention intervention = new Intervention();
        intervention.setUserId(0L);
        intervention.setDeviceId(0L);
        intervention.setHealthData(null);
        intervention.setIntervention("no_existed");
        intervention.setCreateTime(new Date());
        return intervention;
    }
}
